package com.kaifamiao.contraller;

import com.kaifamiao.model.ShijianduanModel;
import com.kaifamiao.model.UserModel;
import com.kaifamiao.service.ShijianduanService;
import com.kaifamiao.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
public class ShijianduanController {
    @Autowired
    private ShijianduanService service;

    @RequestMapping("/addSH")
    public void add(){
        ShijianduanModel model=new ShijianduanModel();
        model.setId(9L);
        model.setTimename("aa");
        model.setBegtime(null);
        model.setEndtime(null);
        System.out.println(service.addShijianduan(model));
    }

    @RequestMapping("/updateSH")
    @CrossOrigin("*")
    public ResponseUtil update(ShijianduanModel model1){
        ShijianduanModel model=new ShijianduanModel();
        model.setId(model1.getId());
        model.setTimename(model1.getTimename());
        model.setBegtime(model1.getBegtime());
        model.setEndtime(model1.getEndtime());
        int row= service.updateShijianduan(model);
        if(row>0){
            return ResponseUtil.ok("修改成功");
        }else{
            return ResponseUtil.error("修改失败");
        }
    }

    @RequestMapping("/deleteSH")
    @CrossOrigin("*")
    public ResponseUtil delete(ShijianduanModel model){
        Long id= model.getId();
        int row= service.deleteShijianduan(id);
        if(row==1){
            return ResponseUtil.ok("删除成功");
        }else{
            return ResponseUtil.error("删除失败");
        }
    }


    @RequestMapping("/shiJianDuanFindAll")
    @CrossOrigin("*")
    public ResponseUtil findAll(ShijianduanModel model){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        String dateStringParse = sdf.format(date);
        model.setBegtime(dateStringParse);

        List<ShijianduanModel> list=service.findAll();
        return ResponseUtil.ok("",list);
    }
}
